package woaini.fenger.core.dispatcher.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.stereotype.Component;
import woaini.fenger.core.base.boot.ApplicationStartupCompleted;
import woaini.fenger.core.bot.event.BaseEvent;
import woaini.fenger.core.bot.event.message.GroupMessageEvent;
import woaini.fenger.core.bot.session.Session;
import woaini.fenger.core.dispatcher.IBotInterceptor;

/**
 * 消息拦截器
 *
 * @see MessageInterceptor
 * @author yefeng {@code @Date} 2023-05-16 16:50:39
 */
@Component
public class MessageInterceptor implements IBotInterceptor, ApplicationStartupCompleted {

  @Override
  public void onInit() {}

  @Override
  public boolean dispatch(Session session) {
    BaseEvent event = session.getEvent();
    String actualText = session.getActualText();
    if (StrUtil.isEmpty(actualText)){
     return true;
    }
    //群聊的话需要at
    if (event instanceof GroupMessageEvent groupMessageEvent) {
      Boolean at = groupMessageEvent.getMessage().isAt(session.getBot().getSelfId());
      if (!at){
        return true;
      }
    }
    //发送消息
    SpringUtil.publishEvent(session);
    return IBotInterceptor.super.dispatch(session);
  }

  @Override
  public int order() {
    return CmdInterceptor.ORDER + 1;
  }
}
